package com.vasthu.utility;

import java.sql.*;

public class VstDBManager {

	private boolean isOpen = false;
	private Connection conn = null;
	private static final VstDBManager inst = new VstDBManager();
	
	private final String URL = "jdbc:mysql://localhost:3306/poss";
	private final String USER = "root";
	private final String PASSWORD = "";
	private final String DRIVER = "com.mysql.jdbc.Driver";
	
	private VstDBManager() {
	}
	
	public static VstDBManager get() {
		return inst;
	}
	
	public Connection getConnection() {
		if (isOpen) {
			System.out.println("ERROR : VstDBManager->getConnection() # You have already an open connection");
			return null;
		}
		try {
			Class.forName(DRIVER); 
			conn = DriverManager.getConnection(URL, USER, PASSWORD);
			isOpen = true;
		} catch (Exception e) {
			System.out.println("ERROR : VstDBManager->getConnection() # " + e.getMessage());
		}
		return conn;
	}
	
	public void close() {
		try {
			if (conn != null) {
				conn.close();
				isOpen = false;
			}
        } catch (Exception ex) {
			System.out.println("ERROR : VstDBManager->close() # Cannot close the connection");
		}
	}
	
}